home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / builtins / exit.def < prev    next >
Encoding:
Text File  |  1994-03-03  |  3.3 KB  |  129 lines

  1. This file is exit.def, from which is created exit.c.
  2. It implements the builtins "bye" and  "exit", and "logout" in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES exit.c
  23.  
  24. $BUILTIN exit
  25. $FUNCTION exit_builtin
  26. $SHORT_DOC exit [n]
  27. Exit the shell with a status of N.  If N is omitted, the exit status
  28. is that of the last command executed.
  29. $END
  30.  
  31. #include <stdio.h>
  32. #include <sys/types.h>
  33. #include "../shell.h"
  34. #include "../jobs.h"
  35.  
  36. #include "builtext.h"    /* for jobs_builtin */
  37.  
  38. extern int interactive, login_shell;
  39. extern int last_command_exit_value;
  40.  
  41. static int exit_or_logout ();
  42.  
  43. int
  44. exit_builtin (list)
  45.      WORD_LIST *list;
  46. {
  47.   if (interactive)
  48.     {
  49.       fprintf (stderr, login_shell ? "logout\n" : "exit\n");
  50.       fflush (stderr);
  51.     }
  52.  
  53.   return (exit_or_logout (list));
  54. }
  55.  
  56. $BUILTIN logout
  57. $FUNCTION logout_builtin
  58. $SHORT_DOC logout
  59. Logout of a login shell.
  60. $END
  61.  
  62. /* How to logout. */
  63. int
  64. logout_builtin (list)
  65.      WORD_LIST *list;
  66. {
  67.   if (!login_shell && interactive)
  68.     {
  69.       builtin_error ("Not login shell: use `exit' or `bye'");
  70.       return (EXECUTION_FAILURE);
  71.     }
  72.   else
  73.     return (exit_or_logout (list));
  74. }
  75.  
  76. /* Clean up work for exiting or logging out. */
  77. Function *last_shell_builtin = (Function *)NULL;
  78. Function *this_shell_builtin = (Function *)NULL;
  79.  
  80. static int
  81. exit_or_logout (list)
  82.      WORD_LIST *list;
  83. {
  84.   int exit_value;
  85.  
  86. #if defined (JOB_CONTROL)
  87.   int exit_immediate_okay;
  88.  
  89.   exit_immediate_okay = (!interactive ||
  90.              last_shell_builtin == exit_builtin ||
  91.              last_shell_builtin == logout_builtin ||
  92.              last_shell_builtin == jobs_builtin);
  93.  
  94.   /* Check for stopped jobs if the user wants to. */
  95.   if (!exit_immediate_okay)
  96.     {
  97.       register int i;
  98.       for (i = 0; i < job_slots; i++)
  99.     if (jobs[i] && (jobs[i]->state == JSTOPPED))
  100.       {
  101.         fprintf (stderr, "There are stopped jobs.\n");
  102.  
  103.         /* This is NOT superfluous because EOF can get here without
  104.            going through the command parser.  Set both last and this
  105.            so that either `exit', `logout', or ^D will work to exit
  106.            immediately if nothing intervenes. */
  107.         this_shell_builtin = last_shell_builtin = exit_builtin;
  108.         return (EXECUTION_FAILURE);
  109.       }
  110.     }
  111. #endif /* JOB_CONTROL */
  112.  
  113.   /* Get return value if present.  This means that you can type
  114.      `logout 5' to a shell, and it returns 5. */
  115.   if (list)
  116.     exit_value = get_numeric_arg (list);
  117.   else
  118.     exit_value = last_command_exit_value;
  119.  
  120.   /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
  121.   if (login_shell)
  122.     maybe_execute_file ("~/.bash_logout", 1);
  123.  
  124.   last_command_exit_value = exit_value;
  125.  
  126.   /* Exit the program. */
  127.   longjmp (top_level, EXITPROG);
  128. }
  129.